<!DOCTYPE HTML>
<html lang="zh-CN">


<head><meta name="generator" content="Hexo 3.9.0">
    <meta charset="utf-8">
    <meta name="keywords" content="Zuji-JPA — 定义优于逻辑的Spring Data JPA动态查询扩展库, 心的足迹, azheng">
    <meta name="description" content="官方文档

Zuji-JPA 是一个不用写sql的 Spring Data JPA 增强库，在 Spring Data JPA 的基础上简化开发，目的是让开发者不再去书写冗长的SQL，支持 入参定义式零逻辑 和 极简Java动态链式 两种方">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <title>Zuji-JPA — 定义优于逻辑的Spring Data JPA动态查询扩展库 | 心的足迹</title>
    <link rel="icon" type="image/png" href="/favicon.png">

    <link rel="stylesheet" type="text/css" href="/libs/awesome/css/font-awesome.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/materialize/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/css/my.css">
    <style type="text/css">
        
        code[class*="language-"], pre[class*="language-"] {
            white-space: pre !important;
        }
        
    </style>

    <script src="/libs/jquery/jquery-2.2.0.min.js"></script>
<link rel="stylesheet" href="/css/prism-tomorrow.css" type="text/css"></head>


<body>

<header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper container">
            <div class="brand-logo">
                <a href="/" class="waves-effect waves-light">
                    
                    <img src="/medias/logo.png" class="logo-img" alt="LOGO">
                    
                    <span class="logo-span">心的足迹</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fa fa-navicon"></i></a>
<ul class="right">
    
    <li class="hide-on-med-and-down">
        <a href="/" class="waves-effect waves-light">
            
            <i class="fa fa-home"></i>
            
            <span>首页</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/tags" class="waves-effect waves-light">
            
            <i class="fa fa-tags"></i>
            
            <span>标签</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/categories" class="waves-effect waves-light">
            
            <i class="fa fa-bookmark"></i>
            
            <span>分类</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/archives" class="waves-effect waves-light">
            
            <i class="fa fa-archive"></i>
            
            <span>归档</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/about" class="waves-effect waves-light">
            
            <i class="fa fa-user-circle-o"></i>
            
            <span>关于</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/friends" class="waves-effect waves-light">
            
            <i class="fa fa-address-book"></i>
            
            <span>友情链接</span>
        </a>
    </li>
    
    <li>
        <a href="#searchModal" class="modal-trigger waves-effect waves-light">
            <i id="searchIcon" class="fa fa-search" title="搜索"></i>
        </a>
    </li>
</ul>

<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
        <img src="/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">心的足迹</div>
        <div class="logo-desc">
            
            人生如逆旅，我亦是行人
            
        </div>
    </div>

    

    <ul class="menu-list mobile-menu-list">
        
        <li>
            <a href="/" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-home"></i>
                
                首页
            </a>
        </li>
        
        <li>
            <a href="/tags" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-tags"></i>
                
                标签
            </a>
        </li>
        
        <li>
            <a href="/categories" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-bookmark"></i>
                
                分类
            </a>
        </li>
        
        <li>
            <a href="/archives" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-archive"></i>
                
                归档
            </a>
        </li>
        
        <li>
            <a href="/about" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-user-circle-o"></i>
                
                关于
            </a>
        </li>
        
        <li>
            <a href="/friends" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-address-book"></i>
                
                友情链接
            </a>
        </li>
        
        
        <li><div class="divider"></div></li>
        <li>
            <a href="https://github.com/azhengZJ/zuji-jpa" class="waves-effect waves-light" target="_blank">
                <i class="fa fa-github-square fa-fw"></i>Fork Me
            </a>
        </li>
        
    </ul>
</div>

        </div>

        
            <style>
    .nav-transparent .github-corner {
        display: none !important;
    }

    .github-corner {
        position: absolute;
        z-index: 10;
        top: 0;
        right: 0;
        border: 0;
        transform: scale(1.1);
    }

    .github-corner svg {
        color: #0f9d58;
        fill: #fff;
        height: 64px;
        width: 64px;
    }

    .github-corner:hover .octo-arm {
        animation: a 0.56s ease-in-out;
    }

    .github-corner .octo-arm {
        animation: none;
    }

    @keyframes a {
        0%,
        to {
            transform: rotate(0);
        }
        20%,
        60% {
            transform: rotate(-25deg);
        }
        40%,
        80% {
            transform: rotate(10deg);
        }
    }
</style>

<a href="https://github.com/azhengZJ/zuji-jpa" class="github-corner tooltipped hide-on-med-and-down" target="_blank"
   data-tooltip="Fork Me" data-position="left" data-delay="50">
    <svg viewBox="0 0 250 250" aria-hidden="true">
        <path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path>
        <path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2"
              fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path>
        <path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z"
              fill="currentColor" class="octo-body"></path>
    </svg>
</a>
        
    </nav>

</header>





<div class="bg-cover pd-header post-cover" style="background-image: url('/medias/banner/1.jpg')">
    <div class="container">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <div class="description center-align post-title">
                        Zuji-JPA — 定义优于逻辑的Spring Data JPA动态查询扩展库
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>



<main class="post-container content">

    
    <link rel="stylesheet" href="/libs/tocbot/tocbot.css">
<style>
    #articleContent h1::before,
    #articleContent h2::before,
    #articleContent h3::before,
    #articleContent h4::before,
    #articleContent h5::before,
    #articleContent h6::before {
        display: block;
        content: " ";
        height: 100px;
        margin-top: -100px;
        visibility: hidden;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        padding-left: 20px;
    }

    .toc-widget .toc-title {
        margin: 35px 0 15px 0;
        padding-left: 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #toc-content .is-active-link::before {
        background-color: #42b983;
    }
</style>
<div class="row">
    <div class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                    <div class="article-tag">
                        
                            <a href="/tags/Java/" target="_blank">
                                <span class="chip bg-color">Java</span>
                            </a>
                        
                            <a href="/tags/JPA/" target="_blank">
                                <span class="chip bg-color">JPA</span>
                            </a>
                        
                            <a href="/tags/Zuji/" target="_blank">
                                <span class="chip bg-color">Zuji</span>
                            </a>
                        
                    </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                    <div class="post-cate">
                        <i class="fa fa-bookmark fa-fw icon-category"></i>
                        
                            <a href="/categories/后端/" class="post-category" target="_blank">
                                后端
                            </a>
                        
                    </div>
                    
                </div>
            </div>

            <div class="post-info">
                <div class="post-date info-break-policy">
                    <i class="fa fa-calendar-minus-o fa-fw"></i>发布日期:&nbsp;&nbsp;
                    2019-10-08
                </div>

                
                    
                    <div class="info-break-policy">
                        <i class="fa fa-file-word-o fa-fw"></i>文章字数:&nbsp;&nbsp;
                        2.2k
                    </div>
                    

                    
                    <div class="info-break-policy">
                        <i class="fa fa-clock-o fa-fw"></i>阅读时长:&nbsp;&nbsp;
                        9 分
                    </div>
                    
                
				
				
                    <div id="busuanzi_container_page_pv" class="info-break-policy">
                        <i class="fa fa-eye fa-fw"></i>阅读次数:&nbsp;&nbsp;
                        <span id="busuanzi_value_page_pv"></span>
                    </div>
				
            </div>
        </div>
        <hr class="clearfix">
        <div class="card-content article-card-content">
            <div id="articleContent">
                <p><a href="https://azhengzj.github.io/zuji-jpa/">官方文档</a></p>
<blockquote>
<p><a href="https://github.com/azhengZJ/zuji-jpa" target="_blank" rel="noopener">Zuji-JPA</a> 是一个不用写sql的 <code>Spring Data JPA</code> 增强库，在 Spring Data JPA 的基础上简化开发，目的是让开发者不再去书写冗长的SQL，支持 <code>入参定义式零逻辑</code> 和 <code>极简Java动态链式</code> 两种方式来代替SQL。</p>
</blockquote>
<h2 id="初衷"><a href="#初衷" class="headerlink" title="初衷"></a>初衷</h2><p>由于spring data jpa 在复杂查询这块的短板，我基于specification 功能 开发出了为简化开发而生的Spring Data JPA 增强库 <code>zuji-jpa</code> 框架。</p>
<h2 id="特性"><a href="#特性" class="headerlink" title="特性"></a>特性</h2><ul>
<li>无侵入性，只做增强不做改变；</li>
<li>超轻量，jar包只有60K，更好用的spring-data-jpa增强库；</li>
<li>低功耗，全部基于静态工具类方法实现，程序启动无需加载任何Class；</li>
<li>提供了 <code>入参定义式零逻辑</code>（支持join） 和 <code>极简Java动态链式</code> 两种方式替代sql；</li>
<li>支持动态投影（Dynamic Projections），支持自定义实体类（VO/DTO）查询返回；</li>
<li>单层级的动态条件查询只需定义入参实体类，不用写具体java实现代码，也不用写sql，即可信手拈来；</li>
<li>多层级嵌套复杂的动态条件查询使用超简洁的动态链式编程即可轻松实现；</li>
<li>使用zuji-jpa可以大大简化开发、提高效率，节省更多的时间让你专注于业务；</li>
</ul>
<h2 id="支持场景"><a href="#支持场景" class="headerlink" title="支持场景"></a>支持场景</h2><p>适用于 Java <code>Spring Data JPA</code> 项目，<code>JDK 1.8</code> 及以上，Spring Data JPA 版本无具体要求。</p>
<p>使用之前需要对<code>Spring Data JPA</code>框架有一定的了解和使用，如果还未使用过，请先写个简单的DEMO熟悉一下，具体可参考<a href="https://spring.io/projects/spring-data-jpa" target="_blank" rel="noopener">Spring Data JPA</a>官方文档。</p>
<h2 id="Spring-项目集成"><a href="#Spring-项目集成" class="headerlink" title="Spring 项目集成"></a>Spring 项目集成</h2><p>如果你是 Spring 项目，请直接集成zuji-jpa依赖，spring boot和spring mvc项目均支持。</p>
<p>gradle</p>
<pre class=" language-groovy"><code class="language-groovy">implementation <span class="token string">'top.spring-data-jpa:zuji-jpa:1.0.1'</span></code></pre>
<p>maven</p>
<pre class=" language-xml"><code class="language-xml"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>top.spring-data-jpa<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>zuji-jpa<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>1.0.1<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span></code></pre>
<h1 id="快速开始"><a href="#快速开始" class="headerlink" title="快速开始"></a>快速开始</h1><p>Zuji-JPA查询全部基于Specification进行扩展增强开发，所以使用之前必须要继承JpaSpecificationExecutor接口</p>
<pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">BlogRepository</span> <span class="token keyword">extends</span> <span class="token class-name">JpaSpecificationExecutor</span><span class="token operator">&lt;</span>Blog<span class="token operator">></span> <span class="token punctuation">{</span>

<span class="token punctuation">}</span></code></pre>
<p>或者使用Zuji-JPA自带的BaseRepository，BaseRepository继承了JpaSpecificationExecutor和JpaRepository接口。</p>
<pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">BlogRepository</span> <span class="token keyword">extends</span> <span class="token class-name">BaseRepository</span><span class="token operator">&lt;</span>Blog<span class="token operator">></span> <span class="token punctuation">{</span>

<span class="token punctuation">}</span></code></pre>
<h2 id="1、单层多条件查询"><a href="#1、单层多条件查询" class="headerlink" title="1、单层多条件查询"></a>1、单层多条件查询</h2><blockquote>
<p>入参定义式零逻辑</p>
</blockquote>
<p>入参定义式查询仅支持单层条件查询，支持JOIN，支持equal、like、in、between等这些常用的查询关键字，多层嵌套复杂查询请参考下一节java动态链式查询。</p>
<p>首先要定义查询入参的实体类，如果字段为NULL则不参与条件查询，默认使用的是等于equal，如果是Collection类型的字段，默认使用的是 IN 查询，<br>也可以使用@QueryOperator 注解里面的 <code>fieldName</code> 字段来定义对应数据库的字段名称。</p>
<pre class=" language-java"><code class="language-java"><span class="token comment" spellcheck="true">/**
 *  如果字段为null则不参与条件查询，默认使用的是等于equal，
 *  如果是Collection类型的字段，默认使用的是 IN 查询
 */</span>
<span class="token annotation punctuation">@Data</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ReqBlogQueryVO</span> <span class="token punctuation">{</span>
    <span class="token comment" spellcheck="true">/**
     *  此注解用于忽略查询
     */</span>
    <span class="token annotation punctuation">@QueryIgnore</span>
    <span class="token keyword">private</span> Long id<span class="token punctuation">;</span>
    <span class="token comment" spellcheck="true">/**
     *  此注解等同于SQL: title like "value%"
     */</span>
    <span class="token annotation punctuation">@QueryOperator</span><span class="token punctuation">(</span>Operator<span class="token punctuation">.</span>STARTING_WITH<span class="token punctuation">)</span>
    <span class="token keyword">private</span> String title<span class="token punctuation">;</span>
    <span class="token comment" spellcheck="true">/**
     *  属于Collection类型，所以默认使用的是IN查询
     */</span>
    <span class="token keyword">private</span> List<span class="token operator">&lt;</span>String<span class="token operator">></span> author<span class="token punctuation">;</span>
    <span class="token comment" spellcheck="true">/**
     *  此注解等同于SQL: content like "%value%"
     */</span>
    <span class="token annotation punctuation">@QueryOperator</span><span class="token punctuation">(</span>Operator<span class="token punctuation">.</span>CONTAINS<span class="token punctuation">)</span>
    <span class="token keyword">private</span> String content<span class="token punctuation">;</span>
    <span class="token comment" spellcheck="true">/**
     *  无注解默认使用的是等于
     */</span>
    <span class="token keyword">private</span> Integer status<span class="token punctuation">;</span>
    <span class="token comment" spellcheck="true">/**
     *  LEFT_JOIN的方式有两种，下面使用的是第二种
     */</span>
    <span class="token annotation punctuation">@JoinColumn</span><span class="token punctuation">(</span>name<span class="token operator">=</span><span class="token string">"createUser"</span><span class="token punctuation">)</span>
    <span class="token annotation punctuation">@QueryOperator</span><span class="token punctuation">(</span>Operator<span class="token punctuation">.</span>CONTAINS<span class="token punctuation">)</span>
    <span class="token keyword">private</span> String userName<span class="token punctuation">;</span>


<span class="token punctuation">}</span></code></pre>
<p>入参定义式查询支持JOIN，LEFT_JOIN的方式有两种，以下任选其一即可。</p>
<p>  1、@QueryOperator注解指定实体类JOIN别名，如 @QueryOperator(fieldName=”createUser.userName”,value=Operator.CONTAINS)</p>
<p>  2、@JoinColumn注解指定JOIN的关联字段名，如 @JoinColumn(name=”createUser”)</p>
<p>由于Spring Data Jpa是基于Hibernate开发的，所以JOIN还是继承了Hibernate面向对象的方式，需要在实体类里面定义好关联关系。</p>
<blockquote>
<p><strong>注</strong>：如果开启了自动建表而又不想在数据库创建外键关联的话需要加上注解<br>    @JoinColumn(foreignKey = @ForeignKey(NO_CONSTRAINT))。</p>
</blockquote>
<pre class=" language-java"><code class="language-java"><span class="token annotation punctuation">@Data</span>
<span class="token annotation punctuation">@Entity</span>
<span class="token annotation punctuation">@FieldNameConstants</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Blog</span> <span class="token punctuation">{</span>
    <span class="token comment" spellcheck="true">//......其他属性省略</span>
    <span class="token comment" spellcheck="true">/**
     * 定义和User表的关联关系
     */</span>
    <span class="token annotation punctuation">@ManyToOne</span>
    <span class="token annotation punctuation">@JoinColumn</span><span class="token punctuation">(</span>foreignKey <span class="token operator">=</span> <span class="token annotation punctuation">@ForeignKey</span><span class="token punctuation">(</span>NO_CONSTRAINT<span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token keyword">private</span> User createUser<span class="token punctuation">;</span>

<span class="token punctuation">}</span></code></pre>
<p>定义好之后就可以直接使用入参实体类生成查询条件，进行查询。</p>
<pre class=" language-java"><code class="language-java"><span class="token annotation punctuation">@RestController</span>
<span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">"/blog"</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">BlogController</span> <span class="token punctuation">{</span>

    <span class="token annotation punctuation">@Autowired</span>
    <span class="token keyword">private</span> BlogRepository repository<span class="token punctuation">;</span>

    <span class="token annotation punctuation">@PostMapping</span><span class="token punctuation">(</span><span class="token string">"/list"</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> Object <span class="token function">list</span><span class="token punctuation">(</span><span class="token annotation punctuation">@RequestBody</span> ReqBlogQueryVO query<span class="token punctuation">)</span><span class="token punctuation">{</span>
        Specification<span class="token operator">&lt;</span>Blog<span class="token operator">></span> spec <span class="token operator">=</span> Specifications<span class="token punctuation">.</span><span class="token function">conditionOf</span><span class="token punctuation">(</span>query<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> repository<span class="token punctuation">.</span><span class="token function">findAll</span><span class="token punctuation">(</span>spec<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre>
<p>等同于如下sql，如果字段值为NULL，则不参与条件查询。</p>
<pre class=" language-sql"><code class="language-sql"><span class="token keyword">SELECT</span> 
    <span class="token number">b</span><span class="token punctuation">.</span><span class="token operator">*</span>
<span class="token keyword">FROM</span>
    blog <span class="token number">b</span> <span class="token keyword">left</span> <span class="token keyword">outer</span> <span class="token keyword">join</span> <span class="token keyword">user</span> u <span class="token keyword">on</span> <span class="token number">b</span><span class="token punctuation">.</span>createUserId <span class="token operator">=</span> u<span class="token punctuation">.</span>id
<span class="token keyword">WHERE</span>
    <span class="token number">b</span><span class="token punctuation">.</span>title <span class="token operator">LIKE</span> <span class="token string">'zuji%'</span>
        <span class="token operator">AND</span> <span class="token number">b</span><span class="token punctuation">.</span>author <span class="token operator">IN</span> <span class="token punctuation">(</span><span class="token string">'azheng1'</span> <span class="token punctuation">,</span> <span class="token string">'azheng2'</span><span class="token punctuation">)</span>
        <span class="token operator">AND</span> <span class="token number">b</span><span class="token punctuation">.</span>content <span class="token operator">LIKE</span> <span class="token string">'%value%'</span>
        <span class="token operator">AND</span> <span class="token number">b</span><span class="token punctuation">.</span><span class="token keyword">status</span> <span class="token operator">=</span> <span class="token number">0</span>
        <span class="token operator">AND</span> <span class="token number">a</span><span class="token punctuation">.</span>user_name <span class="token operator">=</span> <span class="token string">'azheng'</span></code></pre>
<h2 id="2、多层嵌套复杂条件查询"><a href="#2、多层嵌套复杂条件查询" class="headerlink" title="2、多层嵌套复杂条件查询"></a>2、多层嵌套复杂条件查询</h2><blockquote>
<p>java动态链式</p>
</blockquote>
<p>此查询类似于mybatis-plus的条件构造器。<br>以下示例包含：动态条件查询 + or 嵌套条件+ 排序+ 分页</p>
<pre class=" language-java"><code class="language-java"><span class="token annotation punctuation">@RestController</span>
<span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">"/user"</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">UserController</span> <span class="token punctuation">{</span>

    <span class="token annotation punctuation">@Autowired</span>
    <span class="token keyword">private</span> UserRepository repository<span class="token punctuation">;</span>

    <span class="token annotation punctuation">@GetMapping</span><span class="token punctuation">(</span><span class="token string">"/list"</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> Page<span class="token operator">&lt;</span>User<span class="token operator">></span> <span class="token function">list</span><span class="token punctuation">(</span>ReqUserListVO params<span class="token punctuation">)</span> <span class="token punctuation">{</span>
       Specification<span class="token operator">&lt;</span>User<span class="token operator">></span> spec <span class="token operator">=</span> Specifications<span class="token punctuation">.</span><span class="token function">where</span><span class="token punctuation">(</span>e <span class="token operator">-</span><span class="token operator">></span> <span class="token punctuation">{</span>
           <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>params<span class="token punctuation">.</span><span class="token function">getUserType</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>UserType<span class="token punctuation">.</span>ALL<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
               e<span class="token punctuation">.</span><span class="token function">eq</span><span class="token punctuation">(</span><span class="token string">"userType"</span><span class="token punctuation">,</span> params<span class="token punctuation">.</span><span class="token function">getUserType</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
           <span class="token punctuation">}</span>
           <span class="token keyword">if</span> <span class="token punctuation">(</span>params<span class="token punctuation">.</span><span class="token function">getUserName</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">!=</span> null<span class="token punctuation">)</span> <span class="token punctuation">{</span>
               e<span class="token punctuation">.</span><span class="token function">contains</span><span class="token punctuation">(</span><span class="token string">"userName"</span><span class="token punctuation">,</span> params<span class="token punctuation">.</span><span class="token function">getUserName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
           <span class="token punctuation">}</span>
           <span class="token keyword">if</span> <span class="token punctuation">(</span>params<span class="token punctuation">.</span><span class="token function">getRange</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">!=</span> null<span class="token punctuation">)</span> <span class="token punctuation">{</span>
               e<span class="token punctuation">.</span><span class="token function">eq</span><span class="token punctuation">(</span><span class="token string">"assigneeId"</span><span class="token punctuation">,</span> AuthHelper<span class="token punctuation">.</span><span class="token function">currentUserId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
           <span class="token punctuation">}</span>
           e<span class="token punctuation">.</span><span class="token function">or</span><span class="token punctuation">(</span>e2 <span class="token operator">-</span><span class="token operator">></span> <span class="token punctuation">{</span>
               e2<span class="token punctuation">.</span><span class="token function">eq</span><span class="token punctuation">(</span><span class="token string">"status"</span><span class="token punctuation">,</span> <span class="token string">"1"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
               e2<span class="token punctuation">.</span><span class="token function">eq</span><span class="token punctuation">(</span><span class="token string">"status"</span><span class="token punctuation">,</span> <span class="token string">"2"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
           <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
           e<span class="token punctuation">.</span><span class="token function">eq</span><span class="token punctuation">(</span><span class="token string">"deleted"</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
       <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
       Sort sort <span class="token operator">=</span> Sort<span class="token punctuation">.</span><span class="token function">by</span><span class="token punctuation">(</span><span class="token string">"createTime"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">descending</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
       <span class="token keyword">return</span> repository<span class="token punctuation">.</span><span class="token function">findAll</span><span class="token punctuation">(</span>spec<span class="token punctuation">,</span> params<span class="token punctuation">.</span><span class="token function">pageRequest</span><span class="token punctuation">(</span>sort<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre>
<p>判断条件也可以放在方法里面，看起来更简洁一点，如下</p>
<pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> Page<span class="token operator">&lt;</span>User<span class="token operator">></span> <span class="token function">list</span><span class="token punctuation">(</span>ReqUserListVO params<span class="token punctuation">)</span> <span class="token punctuation">{</span>
   Specification<span class="token operator">&lt;</span>User<span class="token operator">></span> spec <span class="token operator">=</span> Specifications<span class="token punctuation">.</span><span class="token function">where</span><span class="token punctuation">(</span>e <span class="token operator">-</span><span class="token operator">></span> <span class="token punctuation">{</span>
       e<span class="token punctuation">.</span><span class="token function">eq</span><span class="token punctuation">(</span><span class="token operator">!</span>params<span class="token punctuation">.</span><span class="token function">getUserType</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>UserType<span class="token punctuation">.</span>ALL<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">"userType"</span><span class="token punctuation">,</span> params<span class="token punctuation">.</span><span class="token function">getUserType</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
       e<span class="token punctuation">.</span><span class="token function">contains</span><span class="token punctuation">(</span>params<span class="token punctuation">.</span><span class="token function">getUserName</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">!=</span> null<span class="token punctuation">,</span> <span class="token string">"userName"</span><span class="token punctuation">,</span> params<span class="token punctuation">.</span><span class="token function">getUserName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
       e<span class="token punctuation">.</span><span class="token function">eq</span><span class="token punctuation">(</span>params<span class="token punctuation">.</span><span class="token function">getRange</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">!=</span> null<span class="token punctuation">,</span> <span class="token string">"assigneeId"</span><span class="token punctuation">,</span> AuthHelper<span class="token punctuation">.</span><span class="token function">currentUserId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
       e<span class="token punctuation">.</span><span class="token function">or</span><span class="token punctuation">(</span>e2 <span class="token operator">-</span><span class="token operator">></span> <span class="token punctuation">{</span>
           e2<span class="token punctuation">.</span><span class="token function">eq</span><span class="token punctuation">(</span><span class="token string">"status"</span><span class="token punctuation">,</span> <span class="token string">"1"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
           e2<span class="token punctuation">.</span><span class="token function">eq</span><span class="token punctuation">(</span><span class="token string">"status"</span><span class="token punctuation">,</span> <span class="token string">"2"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
       <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
       e<span class="token punctuation">.</span><span class="token function">eq</span><span class="token punctuation">(</span><span class="token string">"deleted"</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
   <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
   Sort sort <span class="token operator">=</span> Sort<span class="token punctuation">.</span><span class="token function">by</span><span class="token punctuation">(</span><span class="token string">"createTime"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">descending</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
   <span class="token keyword">return</span> repository<span class="token punctuation">.</span><span class="token function">findAll</span><span class="token punctuation">(</span>spec<span class="token punctuation">,</span> params<span class="token punctuation">.</span><span class="token function">pageRequest</span><span class="token punctuation">(</span>sort<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
<p>如果没有条件判断也可以写成这样，链式编程</p>
<pre class=" language-java"><code class="language-java"><span class="token annotation punctuation">@RestController</span>
<span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">"/user"</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">UserController</span> <span class="token punctuation">{</span>

    <span class="token annotation punctuation">@Autowired</span>
    <span class="token keyword">private</span> UserRepository repository<span class="token punctuation">;</span>

    <span class="token annotation punctuation">@GetMapping</span><span class="token punctuation">(</span><span class="token string">"/list"</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> Page<span class="token operator">&lt;</span>User<span class="token operator">></span> <span class="token function">list</span><span class="token punctuation">(</span>ReqUserListVO params<span class="token punctuation">)</span> <span class="token punctuation">{</span>
       Specification<span class="token operator">&lt;</span>User<span class="token operator">></span> spec <span class="token operator">=</span> Specifications<span class="token punctuation">.</span><span class="token function">where</span><span class="token punctuation">(</span>e <span class="token operator">-</span><span class="token operator">></span> <span class="token punctuation">{</span>
           e<span class="token punctuation">.</span><span class="token function">eq</span><span class="token punctuation">(</span><span class="token string">"userType"</span><span class="token punctuation">,</span> params<span class="token punctuation">.</span><span class="token function">getUserType</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
            <span class="token punctuation">.</span><span class="token function">contains</span><span class="token punctuation">(</span><span class="token string">"userName"</span><span class="token punctuation">,</span> params<span class="token punctuation">.</span><span class="token function">getUserName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
            <span class="token punctuation">.</span><span class="token function">eq</span><span class="token punctuation">(</span><span class="token string">"assigneeId"</span><span class="token punctuation">,</span> AuthHelper<span class="token punctuation">.</span><span class="token function">currentUserId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
            <span class="token punctuation">.</span><span class="token function">or</span><span class="token punctuation">(</span>e2 <span class="token operator">-</span><span class="token operator">></span> e2<span class="token punctuation">.</span><span class="token function">eq</span><span class="token punctuation">(</span><span class="token string">"status"</span><span class="token punctuation">,</span> <span class="token string">"1"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">eq</span><span class="token punctuation">(</span><span class="token string">"status"</span><span class="token punctuation">,</span> <span class="token string">"2"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
            <span class="token punctuation">.</span><span class="token function">eq</span><span class="token punctuation">(</span><span class="token string">"deleted"</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
       <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
       Sort sort <span class="token operator">=</span> Sort<span class="token punctuation">.</span><span class="token function">by</span><span class="token punctuation">(</span><span class="token string">"createTime"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">descending</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
       <span class="token keyword">return</span> repository<span class="token punctuation">.</span><span class="token function">findAll</span><span class="token punctuation">(</span>spec<span class="token punctuation">,</span> params<span class="token punctuation">.</span><span class="token function">pageRequest</span><span class="token punctuation">(</span>sort<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre>
<p>等同于sql</p>
<pre class=" language-sql"><code class="language-sql"><span class="token keyword">SELECT</span>
    <span class="token operator">*</span> 
<span class="token keyword">FROM</span>
    <span class="token keyword">user</span>
<span class="token keyword">WHERE</span>
    user_type <span class="token operator">=</span> <span class="token string">'ADMIN'</span> 
    <span class="token operator">AND</span> user_name <span class="token operator">like</span> <span class="token string">"%admin%"</span> 
    <span class="token operator">AND</span> assignee_id <span class="token operator">=</span> <span class="token number">11</span> 
    <span class="token operator">AND</span> <span class="token punctuation">(</span> <span class="token keyword">status</span> <span class="token operator">=</span> <span class="token number">1</span> <span class="token operator">OR</span> <span class="token keyword">status</span> <span class="token operator">=</span> <span class="token number">2</span> <span class="token punctuation">)</span> 
    <span class="token operator">AND</span> deleted <span class="token operator">=</span> <span class="token number">0</span> 
<span class="token keyword">ORDER</span> <span class="token keyword">BY</span>
    create_time <span class="token keyword">DESC</span> 
    <span class="token keyword">LIMIT</span> <span class="token number">0</span><span class="token punctuation">,</span><span class="token number">10</span></code></pre>
<blockquote>
<p><strong>注</strong>：如果第一层（最外面层）是OR关联查询，调用where方法的时候需要添加一个参数isConjunction，为true的时候为and连接（默认为true），为false的时候为or连接。</p>
</blockquote>
<pre class=" language-java"><code class="language-java">Specification<span class="token operator">&lt;</span>Blog<span class="token operator">></span> spec <span class="token operator">=</span> Specifications<span class="token punctuation">.</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">,</span> e <span class="token operator">-</span><span class="token operator">></span> <span class="token punctuation">{</span>
    e<span class="token punctuation">.</span><span class="token function">eq</span><span class="token punctuation">(</span>Blog<span class="token punctuation">.</span>Fields<span class="token punctuation">.</span>status<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    e<span class="token punctuation">.</span><span class="token function">eq</span><span class="token punctuation">(</span>Blog<span class="token punctuation">.</span>Fields<span class="token punctuation">.</span>status<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
<pre class=" language-sql"><code class="language-sql"><span class="token keyword">select</span> <span class="token operator">*</span> <span class="token keyword">from</span> blog <span class="token keyword">where</span> <span class="token keyword">status</span> <span class="token operator">=</span> <span class="token number">0</span> <span class="token operator">or</span> <span class="token keyword">status</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span></code></pre>
<blockquote>
<p><strong>注</strong>：上述示例中查询的字段名使用的是字符串，字符串是不被检查的，很容易出错。lombok提供了可以生成和属性名一样的的静态字段内部类的注解，实体类上面添加@FieldNameConstants注解即可使用。</p>
</blockquote>
<pre class=" language-java"><code class="language-java"><span class="token comment" spellcheck="true">//...省略</span>
<span class="token annotation punctuation">@FieldNameConstants</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Blog</span> <span class="token punctuation">{</span>
    <span class="token comment" spellcheck="true">//...省略</span>
<span class="token punctuation">}</span></code></pre>
<pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> Page<span class="token operator">&lt;</span>User<span class="token operator">></span> <span class="token function">list</span><span class="token punctuation">(</span>ReqUserListVO params<span class="token punctuation">)</span> <span class="token punctuation">{</span>
   Specification<span class="token operator">&lt;</span>User<span class="token operator">></span> spec <span class="token operator">=</span> Specifications<span class="token punctuation">.</span><span class="token function">where</span><span class="token punctuation">(</span>e <span class="token operator">-</span><span class="token operator">></span> <span class="token punctuation">{</span>
       e<span class="token punctuation">.</span><span class="token function">eq</span><span class="token punctuation">(</span>User<span class="token punctuation">.</span>Fields<span class="token punctuation">.</span>userType<span class="token punctuation">,</span> params<span class="token punctuation">.</span><span class="token function">getUserType</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">contains</span><span class="token punctuation">(</span>User<span class="token punctuation">.</span>Fields<span class="token punctuation">.</span>userName<span class="token punctuation">,</span> params<span class="token punctuation">.</span><span class="token function">getUserName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">eq</span><span class="token punctuation">(</span>User<span class="token punctuation">.</span>Fields<span class="token punctuation">.</span>assigneeId<span class="token punctuation">,</span> AuthHelper<span class="token punctuation">.</span><span class="token function">currentUserId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">or</span><span class="token punctuation">(</span>e2 <span class="token operator">-</span><span class="token operator">></span> e2<span class="token punctuation">.</span><span class="token function">eq</span><span class="token punctuation">(</span>User<span class="token punctuation">.</span>Fields<span class="token punctuation">.</span>status<span class="token punctuation">,</span> <span class="token string">"1"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">eq</span><span class="token punctuation">(</span>User<span class="token punctuation">.</span>Fields<span class="token punctuation">.</span>status<span class="token punctuation">,</span> <span class="token string">"2"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">eq</span><span class="token punctuation">(</span>User<span class="token punctuation">.</span>Fields<span class="token punctuation">.</span>deleted<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
   <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
   Sort sort <span class="token operator">=</span> Sort<span class="token punctuation">.</span><span class="token function">by</span><span class="token punctuation">(</span><span class="token string">"createTime"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">descending</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
   <span class="token keyword">return</span> repository<span class="token punctuation">.</span><span class="token function">findAll</span><span class="token punctuation">(</span>spec<span class="token punctuation">,</span> params<span class="token punctuation">.</span><span class="token function">pageRequest</span><span class="token punctuation">(</span>sort<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
<h2 id="3、两者结合使用"><a href="#3、两者结合使用" class="headerlink" title="3、两者结合使用"></a>3、两者结合使用</h2><p>Zuji-Jpa支持将<code>入参定义式</code>和<code>JAVA动态链式</code>两者结合在一起使用，可以面对更多复杂的场景。</p>
<p>首先需要定义入参实体类，具体参考<a href="#_1%e3%80%81%e5%8d%95%e5%b1%82%e5%a4%9a%e6%9d%a1%e4%bb%b6%e6%9f%a5%e8%af%a2">入参定义式</a>，定义好之后直接进行查询；</p>
<pre class=" language-java"><code class="language-java"><span class="token annotation punctuation">@PostMapping</span><span class="token punctuation">(</span><span class="token string">"/list"</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> Object <span class="token function">list</span><span class="token punctuation">(</span><span class="token annotation punctuation">@RequestBody</span> ReqBlogQueryVO query<span class="token punctuation">)</span><span class="token punctuation">{</span>
    Specification<span class="token operator">&lt;</span>Blog<span class="token operator">></span> spec <span class="token operator">=</span> Specifications<span class="token punctuation">.</span><span class="token function">conditionOf</span><span class="token punctuation">(</span>query<span class="token punctuation">,</span>e <span class="token operator">-</span><span class="token operator">></span> <span class="token punctuation">{</span>
        e<span class="token punctuation">.</span><span class="token function">eq</span><span class="token punctuation">(</span>Blog<span class="token punctuation">.</span>Fields<span class="token punctuation">.</span>deleted<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment" spellcheck="true">// 如上，在此添加需要的查询，参考第二节 java动态链式 查询</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> repository<span class="token punctuation">.</span><span class="token function">findAll</span><span class="token punctuation">(</span>spec<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
<h2 id="4、自定义查询返回"><a href="#4、自定义查询返回" class="headerlink" title="4、自定义查询返回"></a>4、自定义查询返回</h2><h3 id="Dynamic-Projections（动态投影）"><a href="#Dynamic-Projections（动态投影）" class="headerlink" title="Dynamic Projections（动态投影）"></a>Dynamic Projections（动态投影）</h3><p>数据库查询大部分时候都要 返回自定义字段（ VO/DTO ），Spring Data Jpa 默认支持 泛型 动态投影（Projections） ，投影的类型可以是接口也可以是类。</p>
<p>Repository查询接口定义</p>
<pre class=" language-java"><code class="language-java"><span class="token keyword">interface</span> <span class="token class-name">PersonRepository</span> <span class="token keyword">extends</span> <span class="token class-name">Repository</span><span class="token operator">&lt;</span>Person<span class="token punctuation">,</span> UUID<span class="token operator">></span> <span class="token punctuation">{</span>
  <span class="token operator">&lt;</span>T<span class="token operator">></span> Collection<span class="token operator">&lt;</span>T<span class="token operator">></span> <span class="token function">findByUsername</span><span class="token punctuation">(</span>String username<span class="token punctuation">,</span> Class<span class="token operator">&lt;</span>T<span class="token operator">></span> type<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
<p>或者</p>
<pre class=" language-java"><code class="language-java"><span class="token keyword">interface</span> <span class="token class-name">PersonRepository</span> <span class="token keyword">extends</span> <span class="token class-name">Repository</span><span class="token operator">&lt;</span>Person<span class="token punctuation">,</span> UUID<span class="token operator">></span> <span class="token punctuation">{</span>
  NamesQueryDTO <span class="token function">findByUsername</span><span class="token punctuation">(</span>String username<span class="token punctuation">,</span> Class<span class="token operator">&lt;</span>T<span class="token operator">></span> type<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
<p><strong>基于接口的投影</strong>，须提供字段对应的get方法接口。</p>
<pre class=" language-java"><code class="language-java"><span class="token keyword">interface</span> <span class="token class-name">NamesQueryDTO</span> <span class="token punctuation">{</span>

  String <span class="token function">getFirstname</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  String <span class="token function">getLastname</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token punctuation">}</span></code></pre>
<p><strong>基于类的投影</strong>，定义好所有查询字段，并且提供全参构造器。强烈推荐 使用Lombok的@Value注解 简化代码。</p>
<pre class=" language-java"><code class="language-java"><span class="token annotation punctuation">@Value</span>
<span class="token keyword">class</span> <span class="token class-name">NamesOnlyDTO</span> <span class="token punctuation">{</span>
    String firstname<span class="token punctuation">,</span> lastname<span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
<h3 id="提供工具类转换"><a href="#提供工具类转换" class="headerlink" title="提供工具类转换"></a>提供工具类转换</h3><p>动态投影有时候会不生效，这时候就需要自己进行类型转换，zuji-jpa提供了相关工具类。</p>
<p>没有分页的列表</p>
<pre class=" language-java"><code class="language-java">List<span class="token operator">&lt;</span>Blog<span class="token operator">></span> list <span class="token operator">=</span> repository<span class="token punctuation">.</span><span class="token function">findAll</span><span class="token punctuation">(</span>spec<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">return</span> EntityUtils<span class="token punctuation">.</span><span class="token function">cast</span><span class="token punctuation">(</span>list<span class="token punctuation">,</span> RespBlogVO<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
<p>有分页的列表</p>
<pre class=" language-java"><code class="language-java">Specification<span class="token operator">&lt;</span>Blog<span class="token operator">></span> spec <span class="token operator">=</span> Specifications<span class="token punctuation">.</span><span class="token function">conditionOf</span><span class="token punctuation">(</span>vo<span class="token punctuation">,</span> e<span class="token operator">-</span><span class="token operator">></span><span class="token punctuation">{</span>
    e<span class="token punctuation">.</span><span class="token function">eq</span><span class="token punctuation">(</span>Blog<span class="token punctuation">.</span>Fields<span class="token punctuation">.</span>status<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
PageRequest page <span class="token operator">=</span> vo<span class="token punctuation">.</span><span class="token function">pageRequest</span><span class="token punctuation">(</span>Sort<span class="token punctuation">.</span><span class="token function">by</span><span class="token punctuation">(</span>Blog<span class="token punctuation">.</span>Fields<span class="token punctuation">.</span>id<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">descending</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
Page<span class="token operator">&lt;</span>Blog<span class="token operator">></span> list <span class="token operator">=</span> repository<span class="token punctuation">.</span><span class="token function">findAll</span><span class="token punctuation">(</span>spec<span class="token punctuation">,</span> page<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">return</span> JpaHelper<span class="token punctuation">.</span><span class="token function">castPage</span><span class="token punctuation">(</span>list<span class="token punctuation">,</span> page<span class="token punctuation">,</span> RespBlogVO<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
<p>或者</p>
<pre class=" language-java"><code class="language-java">Specification<span class="token operator">&lt;</span>Blog<span class="token operator">></span> spec <span class="token operator">=</span> Specifications<span class="token punctuation">.</span><span class="token function">conditionOf</span><span class="token punctuation">(</span>vo<span class="token punctuation">,</span> e<span class="token operator">-</span><span class="token operator">></span><span class="token punctuation">{</span>
    e<span class="token punctuation">.</span><span class="token function">eq</span><span class="token punctuation">(</span>Blog<span class="token punctuation">.</span>Fields<span class="token punctuation">.</span>status<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
Page<span class="token operator">&lt;</span>Blog<span class="token operator">></span> list <span class="token operator">=</span> repository<span class="token punctuation">.</span><span class="token function">findAll</span><span class="token punctuation">(</span>spec<span class="token punctuation">,</span> vo<span class="token punctuation">.</span><span class="token function">pageRequest</span><span class="token punctuation">(</span>Sort<span class="token punctuation">.</span><span class="token function">by</span><span class="token punctuation">(</span>Blog<span class="token punctuation">.</span>Fields<span class="token punctuation">.</span>id<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">descending</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
Page<span class="token operator">&lt;</span>RespBlogVO<span class="token operator">></span> result <span class="token operator">=</span> JpaHelper<span class="token punctuation">.</span><span class="token function">castPage</span><span class="token punctuation">(</span>list<span class="token punctuation">,</span> RespBlogVO<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
<p>单个对象</p>
<pre class=" language-java"><code class="language-java">Blog blog <span class="token operator">=</span> repository<span class="token punctuation">.</span><span class="token function">findOne</span><span class="token punctuation">(</span>id<span class="token punctuation">)</span><span class="token punctuation">;</span>
RespBlogVO result <span class="token operator">=</span>  EntityUtils<span class="token punctuation">.</span><span class="token function">cast</span><span class="token punctuation">(</span>Blog<span class="token punctuation">,</span> RespBlogVO<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
<p>如果有任何问题或想要更多交流，请加QQ群 <code>758629787</code>。</p>
<h2 id="开源许可证"><a href="#开源许可证" class="headerlink" title="开源许可证"></a>开源许可证</h2><p>本 <code>Zuji-JPA</code> 的 Spring Data JPA 扩展库遵守 <a href="http://www.apache.org/licenses/LICENSE-2.0" target="_blank" rel="noopener">Apache License 2.0</a> 许可证。</p>

            </div>
            <hr/>

            
            <style>
    #reward {
        margin: 40px 0;
        text-align: center;
    }

    #reward .reward-link {
        font-size: 1.88rem;
    }

    #reward .btn-floating:hover {
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2), 0 5px 15px rgba(0, 0, 0, 0.2);
    }

    #rewardModal {
        width: 320px;
        height: 350px;
    }

    #rewardModal .reward-title {
        margin: 15px auto;
        padding-bottom: 5px;
    }

    #rewardModal .modal-content {
        padding: 10px;
    }

    #rewardModal .close {
        position: absolute;
        right: 15px;
        top: 15px;
        color: rgba(0, 0, 0, 0.5);
        font-size: 1.3rem;
        line-height: 20px;
        cursor: pointer;
    }

    #rewardModal .close:hover {
        color: #ef5350;
        transform: scale(1.3);
        -moz-transform:scale(1.3);
        -webkit-transform:scale(1.3);
        -o-transform:scale(1.3);
    }

    #rewardModal .reward-tabs {
        margin: 0 auto;
        width: 210px;
    }

    .reward-tabs .tabs {
        height: 38px;
        margin: 10px auto;
        padding-left: 0;
    }

    .reward-content ul {
        padding-left: 0 !important;
    }

    .reward-tabs .tabs .tab {
        height: 38px;
        line-height: 38px;
    }

    .reward-tabs .tab a {
        color: #fff;
        background-color: #ccc;
    }

    .reward-tabs .tab a:hover {
        background-color: #ccc;
        color: #fff;
    }

    .reward-tabs .wechat-tab .active {
        color: #fff !important;
        background-color: #22AB38 !important;
    }

    .reward-tabs .alipay-tab .active {
        color: #fff !important;
        background-color: #019FE8 !important;
    }

    .reward-tabs .reward-img {
        width: 210px;
        height: 210px;
    }
</style>

<div id="reward">
    <a href="#rewardModal" class="reward-link modal-trigger btn-floating btn-large waves-effect waves-light red">赏</a>

    <!-- Modal Structure -->
    <div id="rewardModal" class="modal">
        <div class="modal-content">
            <a class="close modal-close"><i class="fa fa-close"></i></a>
            <h4 class="reward-title">你的赏识是我前进的动力</h4>
            <div class="reward-content">
                <div class="reward-tabs">
                    <ul class="tabs row">
                        <li class="tab col s6 alipay-tab waves-effect waves-light"><a href="#alipay">支付宝</a></li>
                        <li class="tab col s6 wechat-tab waves-effect waves-light"><a href="#wechat">微 信</a></li>
                    </ul>
                    <div id="alipay">
                        <img src="/medias/reward/alipay.jpg" class="reward-img" alt="支付宝打赏二维码">
                    </div>
                    <div id="wechat">
                        <img src="/medias/reward/wechat.png" class="reward-img" alt="微信打赏二维码">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $('.tabs').tabs();
    });
</script>
            

            <link rel="stylesheet" type="text/css" href="/libs/share/css/share.min.css">

<div id="article-share">
    
    <div class="social-share" data-disabled="qzone" data-wechat-qrcode-helper="<p>微信里点“发现”->“扫一扫”二维码便可查看分享。</p>"></div>
    
</div>

<script src="/libs/share/js/social-share.min.js"></script>

            <div class="reprint">
                <p>
                    <span class="reprint-tip">
                        <i class="fa fa-exclamation-circle"></i>&nbsp;&nbsp;转载请注明:
                    </span>
                    <a href="https://azhengZJ.github.io" class="b-link-green">心的足迹</a>
                    <i class="fa fa-angle-right fa-lg fa-fw text-color"></i>
                    <a href="/2019/10/08/hou-duan/jpa/zuji-jpa-ding-yi-you-yu-luo-ji-de-spring-data-jpa-dong-tai-cha-xun-kuo-zhan-ku/" class="b-link-green">Zuji-JPA — 定义优于逻辑的Spring Data JPA动态查询扩展库</a>
                </p>
            </div>
        </div>
    </div>

    
        <link rel="stylesheet" href="/libs/gitalk/gitalk.css">
<link rel="stylesheet" href="/css/my-gitalk.css">

<div class="card gitalk-card" data-aos="fade-up">
    <div id="gitalk-container" class="card-content"></div>
</div>

<script src="/libs/gitalk/gitalk.min.js"></script>
<script>
    let gitalk = new Gitalk({
        clientID: 'd4d1d3ec1e0efc877d7c',
        clientSecret: '21cd95acc3f3b86784b94e73fc7116ce1bff0780',
        repo: 'azhengZJ.github.io',
        owner: 'azhengZJ',
        admin: ["azhengZJ"],
        id: '2019-10-08T14-10-00',
        distractionFreeMode: false  // Facebook-like distraction free mode
    });

    gitalk.render('gitalk-container');
</script>
    

    

    

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="fa fa-chevron-left"></i>&nbsp;上一篇</div>
            <div class="card">
                <a href="/2019/10/10/ruan-jian-gong-ju/git/liao-jie-github-liu-cheng/">
                    <div class="card-image">
                        
                        <img src="/medias/banner/2.jpg" class="responsive-img" alt="了解GitHub流程">
                        
                        <span class="card-title">了解GitHub流程</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary">##了解GitHub流程
GitHub flow是一个轻量级的，基于分支的工作流，它支持定期进行部署的团队和项目。本指南说明了GitHub flow的工作方式和原因。
创建一个分支当您在进行项目时，在任何给定的时间都会有许多不同的功能或想法</div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <i class="fa fa-clock-o fa-fw icon-date"></i>2019-10-10
                        </span>
                        <span class="publish-author">
                            
                            <i class="fa fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/软件工具/" class="post-category" target="_blank">
                                    软件工具
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/Git/" target="_blank">
                        <span class="chip bg-color">Git</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                下一篇&nbsp;<i class="fa fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/2019/09/12/hello-world/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/12.jpg" class="responsive-img" alt="Hello World">
                        
                        <span class="card-title">Hello World</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary">Welcome to Hexo! This is your very first post. Check documentation for more info. If you get any problems when using Hex</div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="fa fa-clock-o fa-fw icon-date"></i>2019-09-12
                            </span>
                        <span class="publish-author">
                            
                            <i class="fa fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/前端/" class="post-category" target="_blank">
                                    前端
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/Hexo/" target="_blank">
                        <span class="chip bg-color">Hexo</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
    </div>
</article>
</div>


    </div>
    <div class="col l3 hide-on-med-and-down">
        <div class="toc-widget">
            <div class="toc-title"><i class="fa fa-list-alt"></i>&nbsp;&nbsp;目录</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<script src="/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            // headingsOffset: -205,
            headingSelector: 'h2, h3, h4'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h2, h3, h4').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });
    });
</script>
    

</main>


<footer class="page-footer bg-color">
    <div class="container row center-align">
        <div class="col s12 m8 l8 copy-right">
            本站由&copy;<a href="https://azhengZJ.github.io/" target="_blank">azheng</a>搭建.

            
                &nbsp;<i class="fa fa-area-chart"></i>&nbsp;站点总字数:&nbsp;
                <span class="white-color">26.8k</span>
            

            
			
                <br>
                
                <span id="busuanzi_container_site_pv">
                    <i class="fa fa-heart-o"></i>
                    本站总访问量 <span id="busuanzi_value_site_pv" class="white-color"></span>
                </span>
                
                
                <span id="busuanzi_container_site_uv">
                    <i class="fa fa-users"></i>
                    次,&nbsp;访客数 <span id="busuanzi_value_site_uv" class="white-color"></span> 人.
                </span>
                
            
        </div>
        <div class="col s12 m4 l4 social-link social-statis">
    <a href="https://github.com/azhengZJ/" class="tooltipped" target="_blank" data-tooltip="访问我的GitHub" data-position="top" data-delay="50">
        <i class="fa fa-github"></i>
    </a>



    <a href="mailto:18055661285@163.com" class="tooltipped" target="_blank" data-tooltip="邮件联系我" data-position="top" data-delay="50">
        <i class="fa fa-envelope-open"></i>
    </a>



    <a href="tencent://AddContact/?fromId=50&fromSubId=1&subcmd=all&uin=1009460334" class="tooltipped" data-tooltip="QQ联系我: 1009460334" data-position="top" data-delay="50">
        <i class="fa fa-qq"></i>
    </a>



    <a href="/atom.xml" class="tooltipped" target="_blank" data-tooltip="RSS 订阅" data-position="top" data-delay="50">
        <i class="fa fa-rss"></i>
    </a>
</div>
    </div>
</footer>

<div class="progress-bar"></div>


<!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fa fa-search"></i>&nbsp;&nbsp;搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字"
                   class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script src="/js/search.js"></script>
<script type="text/javascript">
$(function () {
    searchFunc("/" + "search.xml", 'searchInput', 'searchResult');
});
</script>
<!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fa fa-angle-up"></i>
    </a>
</div>


<script src="/libs/materialize/materialize.min.js"></script>
<script src="/libs/masonry/masonry.pkgd.min.js"></script>
<script src="/libs/aos/aos.js"></script>
<script src="/libs/scrollprogress/scrollProgress.min.js"></script>
<script src="/libs/lightGallery/js/lightgallery-all.min.js"></script>
<script src="/js/matery.js"></script>

<!-- Global site tag (gtag.js) - Google Analytics -->



    <script src="/libs/others/clicklove.js"></script>


    <script async src="/libs/others/busuanzi.pure.mini.js"></script>


</body>
</html>